<?php

	require_once('../includes/config.php');
	require_once('../includes/functions.php');
	
	// Retrieve base HTML
	$pageStr = buildStandardPage('Deposit','Transaction');
	
	$content = '';
	
	if(isMobile()){
		$content .= 'Detected Mobile!';
	}
	else{
		if(false == isset($_POST['submit'])){
			if(isLoggedIn()) {
				if (true == isBankManager()){ 
					$id = getID();
					
					$content .= makeAccountNavBar($id);
					
					$content .= '<form name=depositForm method=post action='. $_SERVER['PHP_SELF'] 
					. ' onSubmit="return validateDepositForm(depositForm)" >'
					.'<table border=1 cellpadding=2 width=650>';
					
					$content .= populateAccountsForTable($id);

					$content .= '<tr><td>Amount $:</td><td><input type=text name=amount></td></tr>';
					$content .= '</table><input type=submit name=submit value=Submit>'
					.'<input type=button name=cancel value=Cancel onclick="window.location=\''. $TRANS_URL . '\'" >'
					.'<input type=reset value=Reset></form>';
					
				}
				else if (true == isAdmin()){
					$content .= $MUST_BE_BANK_MNGR;
				}
				else{ // It's a customer
					$content .= $PLEASE_GOTO_BANK;
				}
			}
			// The user is not logged in.
			else{
			  $content .= $MUST_BE_BANK_EMPLOYEE;
			}
		}
		else{
			$tempArray = cleanPost($_POST);
			
			// accountID-Balance
			$aid = explode("-", $tempArray[$ACCOUNT_ID]);
			
			// update new balance
			$balance = $aid[1] + $tempArray['amount'];			
			$updateBalanceQuery = buildUpdateStatement('account', array('balance'=> $balance), $aid[0]);
			$result = executeSQL($updateBalanceQuery, 'Failed to insert into the database.');
			
			date_default_timezone_set('America/Chicago');
			
			// create the transaction record
			$insertTransactionQuery = buildInsertStatement('transaction', array('accountid' => $aid[0],
																				'type' => 'Deposit',
																				'amount'=> $tempArray['amount'],
																				'balance'=> $balance,
																				'date'=> date("Y-m-d")));
			//echo $insertTransactionQuery;
			$result = executeSQL($insertTransactionQuery, 'Failed to insert into the database.');
			
			header('Location: ' . $ACCOUNTS_URL);
		}
	}

	$pageStr = str_Replace('<!--content-->', $content, $pageStr);

	/**
	 * Send the generated HTML to the client's browser
	 */
	echo $pageStr;
?>